﻿using System;
using System.Collections.Generic;
using System.Web.UI.WebControls;
using Module.Core.LoginSystem.Commons;
using Module.Core.LoginSystem.Commons.Framwork;
using Module.Core.LoginSystem.Commons.Utilities;
using Module.Core.LoginSystem.Components.Audits;
using Module.Core.LoginSystem.Components.Commands;
using Module.Core.LoginSystem.Components.Modules;
using Module.Core.LoginSystem.Components.Roles;
using Module.Core.LoginSystem.Components.Users;

namespace Web.Cms.Roles
{
    public partial class UsersInRole : AdminUserControl
    {
        private int _roleId;
        protected void Page_Load(object sender, EventArgs e)
        {
            _roleId = ConvertUtility.ToInt32(Request.QueryString["usersinroleId"]);
            if (!Page.IsPostBack)
            {
                if (_roleId > 0)
                {
                    RoleInfo _role = RoleController.GetRole(_roleId);
                    lblRoleName.Text = _role.RoleName;
                }
            }
        }

        protected void btnAdd_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (GridViewRow row in grvUsers.Rows)
                {
                    CheckBox chkCheckUser = (CheckBox)row.FindControl("chkCheckUser");
                    if (chkCheckUser.Checked)
                    {
                        int _userId = ConvertUtility.ToInt32(((Label)row.FindControl("lblUserID")).Text);
                        UserController.AddUserToRole(_userId, _roleId);
                    }
                }
                // Insert Cores_Audits
                ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Update, CurrentCommand.CtrlKey, CurrentUser.UserID,
                                           _roleId, "Thay đổi thành viên của nhóm: " + lblRoleName.Text, 1,
                                           DateTime.Now);

            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected void btnRemove_Click(object sender, EventArgs e)
        {
            try
            {
                foreach (GridViewRow row in grvUsersInRole.Rows)
                {
                    CheckBox chkCheckUser = (CheckBox)row.FindControl("chkCheckUser");
                    if (chkCheckUser.Checked)
                    {
                        int _userId = ConvertUtility.ToInt32(((Label)row.FindControl("lblUserIDInRole")).Text);
                        UserController.RemoveUserFromRole(_userId, _roleId);
                    }
                }
                // Insert Cores_Audits
                ModuleInfo _moduleInfo = ModuleController.GetModule(CurrentCommand.CtrlKey);
                AuditController.CreateAuditQ(_moduleInfo.ModuleDefID, (int)AuditCode.Update, CurrentCommand.CtrlKey, CurrentUser.UserID,
                                           _roleId, "Thay đổi thành viên của nhóm: " + lblRoleName.Text, 1,
                                           DateTime.Now);
            }
            catch (Exception ex)
            {
                Response.Write(ex.Message);
            }
        }

        protected override void OnPreRender(EventArgs e)
        {
            base.OnPreRender(e);
            List<UserInfo> _lstUsersInRole = UserController.GetUsers(CurrentPortalID, _roleId);
            grvUsersInRole.DataSource = _lstUsersInRole;
            grvUsersInRole.RowDataBound += new GridViewRowEventHandler(grvUsersInRole_RowDataBound);
            grvUsersInRole.DataBind();
            List<UserInfo> _lstUsers = UserController.GetUsers(CurrentPortalID);
            grvUsers.DataSource = _lstUsers;
            grvUsers.RowDataBound += new GridViewRowEventHandler(grvUsers_RowDataBound);
            grvUsers.DataBind();
            foreach (GridViewRow row in grvUsers.Rows)
            {
                Label lblUserID = (Label)row.FindControl("lblUserID");
                foreach (GridViewRow rows in grvUsersInRole.Rows)
                {
                    Label lblUserIDInRole = (Label)rows.FindControl("lblUserIDInRole");
                    if (lblUserID.Text == lblUserIDInRole.Text)
                    {
                        row.Visible = false;
                        continue;
                    }
                }
            }
            CommandInfo _commandInfo = CommandController.GetCommand("admin_role");
            RolePermisionInfo _roleInfo =
                    RoleController.GetReadWritePermison(CurrentUser.UserID, _commandInfo.CommandID);
            if (_roleInfo == null) return;
            if (_roleInfo.Write == 0)
            {
                btnAdd.Visible = false;
                btnRemove.Visible = false;
            }
        }

        protected void grvUsers_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chkCheckedUsers = (CheckBox)e.Row.FindControl("checkUser");
                chkCheckedUsers.Attributes.Add("onclick", "getUsers('" + chkCheckedUsers.ClientID + "')");
            }
        }

        protected void grvUsersInRole_RowDataBound(object sender, GridViewRowEventArgs e)
        {
            if (e.Row.RowType == DataControlRowType.Header)
            {
                CheckBox chkCheckUsers = (CheckBox)e.Row.FindControl("checkedUserInRole");
                chkCheckUsers.Attributes.Add("onclick", "getUserInRole('" + chkCheckUsers.ClientID + "')");
            }
        }

        protected void grvUsersInRole_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvUsersInRole.PageIndex = e.NewPageIndex;
        }

        protected void grvUsers_PageIndexChanging(object sender, GridViewPageEventArgs e)
        {
            grvUsers.PageIndex = e.NewPageIndex;
        }

        protected void btnBack_Click(object sender, EventArgs e)
        {
            Response.Redirect(UIProcess.GetCPPage(CurrentPortalID, "admin_role", new string[] { }, new string[] { }));
        }
    }
}